package org.grow.bill.service;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: xwg
 * @CreateDate: 21-8-17
 */

@Service
public class FeignWithCookie implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =
                requestAttributes.getRequest();

        Cookie[] cookies = request.getCookies();
        if (cookies == null) return;

        for (Cookie cookie : cookies) {
            if (cookie.getName().toLowerCase().contains("session")){
                requestTemplate.header("Cookie",cookie.getName()+"="+cookie.getValue());
//                requestTemplate.header("Cookie",request.getHeader("Cookie"));
            }
        }



    }
}
